<template>
	<view class="ch-modal ch-mask" :style="{'z-index':zIndex}" v-show="value" @tap.stop="closeByMask">
		<view class="ch-wrap" @tap.stop>
			<view v-if="tips" class="ch-tips" :style="tipsStyle">{{tips}}</view>
			<scroll-view class="ch-scroll-view" scroll-y="true">
				<block v-for="(item, index) in list" :key="index">
					<view @tap="itemClick(index)" :style="itemStyle" class="ch-item" :hover-stay-time="150">
						<text class="ch-text" :class="{'ch-disabled':item.disabled}">{{item.text}}</text>
						<text class="ch-sub-text" v-if="item.subText">{{item.subText}}</text>
					</view>
				</block>
			</scroll-view>
			<view v-if="showCancle" class="ch-cancel" :style="cancleStyle" @tap.stop="close">
				{{cancleText}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 通过双向绑定控制组件的弹出与收起
			value: {
				type: Boolean,
				default: false
			},
			// 列表项
			list: {
				type: Array,
				default: () => []
			},
			// 列表项的样式
			itemStyle: {
				type: Object,
				default: () => {}
			},
			// 顶部的提示文字
			tips: {
				type: String,
				default: ''
			},
			// 顶部提示的样式
			tipsStyle: {
				type: Object,
				default: () => {}
			},
			// 是否显示底部的取消按钮
			showCancle: {
				type: Boolean,
				default: true
			},
			// 点击遮罩是否可以关闭
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 取消按钮的提示文字
			cancleText: {
				type: String,
				default: '取消'
			},
			// 取消按钮的样式
			cancleStyle: {
				type: Object,
				default: () => {}
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 1024
			},
		},
		data() {
			return {

			}
		},
		methods: {
			closeByMask() {
				if (!this.maskCloseAble) {
					return
				}

				this.close()
			},
			// 点击取消按钮
			close() {
				// 发送input事件
				this.$emit('close');
				this.$emit('input', false);
			},
			// 点击某一个item
			itemClick(index) {
				// disabled禁止点击
				if (this.list[index].disabled) return;
				this.$emit('click', index);
				this.$emit('input', false);
			}
		}
	}
</script>

<style>
	.ch-modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		opacity: 0;
	}

	.ch-mask {
		opacity: 1;
		background-color: rgba(0, 0, 0, 0.6);
		transition: all 0.25s ease-in-out 0s;
	}

	.ch-wrap {
		position: absolute;
		bottom: 0;
		width: 100%;
		background-color: #eaeaec;
	}

	.ch-tips {
		font-size: 26rpx;
		text-align: center;
		padding: 34rpx;
		line-height: 1.5;
		color: #909399;
		background-color: #FFFFFF;
	}

	.ch-scroll-view {
		max-height: 70vh;
		background-color: #FFFFFF;
	}

	.ch-item {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		line-height: 1;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		padding: 34rpx;
		border-bottom: 1px solid #e4e7ed;
	}

	.ch-item:last-child {
		border: 0;
	}

	.ch-text {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		/* 限制在一个块元素显示的文本的行数 */
		-webkit-box-orient: vertical;
		/* 垂直排列 */
		word-break: break-all;
		/* 内容自动换行 */
	}

	.ch-sub-text {
		font-size: 24rpx;
		color: #909399;
		margin-top: 20rpx;
	}

	.ch-cancel {
		height: 44px;
		line-height: 44px;
		margin-top: 12rpx;
		padding: 0 34rpx;
		color: #303133;
		text-align: center;
		background-color: #FFFFFF;
	}

	.ch-disabled {
		color: #bdbdbd;
	}
</style>
